import socket from "@/socket";

const state = {
    applications: [],                   // 好友申请
    unreadApplications: false,          // 有未读好友申请
}
  
const mutations = {
    pushApplications(state, data) {
        this.commit('newFriend/upUnreadApplications', true);
        if (data?.length === undefined)
        {
            state.applications.push(data);
            return;
        }
        data.forEach(item => state.applications.push(item));
    },
    // 改变未读好友申请
    upUnreadApplications(state, boolean) {
        state.unreadApplications = boolean;
    },
    // 改变该条申请状态
    upApplicationStatus(state, data) {
        let { applications_id, status } = data;
        console.log(data);
        
        state.applications.find(item => item._id == applications_id).status = status;
    },
}

const actions = {
    handleFriendRequest(context, data) {
        return new Promise((resolve, reject) => {
            socket.emit('handle_friendRequest', data, res => resolve(res));
        })
    },
    // 获取好友申请
    getApplications(context, data) {
        return new Promise((resolve, reject) => {
            socket.emit('get_applications', data, res =>  resolve(res));
        })
    },
}

export default {
    namespaced: true,
    state,
    mutations,
    actions
}